#include "study_widget_top.h"
#include "stydatabase.h"
#include "format.h"

study_widget_top::study_widget_top(QWidget *parent) : QWidget(parent)
{
    /* 1.实例化 */
    top_layout = new QHBoxLayout;
    portrait = new QLabel;      //肖像
    nameLabel = new QLabel(g_data[0].friends.UNickname);//姓名
    statusLabel = new QLabel("离线");
    statusBtn = new QPushButton();

    /* 2.设置大小及布局 */
    portrait->setObjectName("portrait");
    portrait->setMargin(5);
    nameLabel->setObjectName("nameLabel");
    statusBtn->setObjectName("statusBtn");
    statusLabel->setObjectName("statusLabel");
    this->setMinimumHeight(45);
    this->setMaximumHeight(45);
    this->setObjectName("study_widget_top");

    portrait->resize(35,35);
    statusBtn->resize(15,15);
    QPixmap pixmap(":img/img/portrait.png");
    QPixmap img=pixmap.scaled(portrait->size(),Qt::KeepAspectRatio);
    portrait->setPixmap(img);

    top_layout->addSpacing(12);
    top_layout->addWidget(statusBtn);
    top_layout->addWidget(statusLabel);
    top_layout->addStretch();
    top_layout->addWidget(portrait);
    top_layout->addWidget(nameLabel);
    top_layout->addSpacing(12);
    this->setLayout(top_layout);

    top_layout->setSpacing(0);
    top_layout->setMargin(0);             //设置外边距

    this->setContentsMargins(0,0,0,0);
    statusBtn->setStyleSheet("\
                                background-color: rgb(230, 230, 230);\
                                width: 15px;\
                                height: 15px;\
                              \
                             hover{\
                                background-color: rgb(255, 255, 255);\
                                width: 17px;\
                                height: 17px;\
                             }");
    statusBtn->setToolTip("电源未接通或网络问题!");
    this->setStyleSheet("padding-right:8px;");
}

void study_widget_top::paintEvent(QPaintEvent *event)
{
    statusBtn->resize(15,15);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void study_widget_top::onPenStatusChanged(qint64 status)
{
    if(status == -1){
        statusBtn->setToolTip("电源未接通或网络问题!");
        statusLabel->setText("离线");
        statusBtn->setStyleSheet("background-color: rgb(230, 230, 230);");

    }else if(status == 0){
        statusBtn->setToolTip("握笔姿势不正确或传感器问题!");
        statusLabel->setText("在线");
        statusBtn->setStyleSheet("background-color: rgb(227, 243, 9);");
    }else{
        statusBtn->setToolTip("正在学习!");
        statusLabel->setText("正在学习");
        statusBtn->setStyleSheet("background-color: rgb(0, 206, 38);");
        emit sigPenStudy(status);
    }
}
